//
//  UIButton+Create.m
//  C2CMember
//
//  Created by yangy on 2018/7/2.
//  Copyright © 2018年 iphone. All rights reserved.
//

#import "UIButton+Create.h"

@implementation UIButton (Create)

+ (UIButton*) cm_ButtonWithTitle:(NSString*) title
                            font:(UIFont*) font
                      titleColor:(UIColor*) titColor
{
    return [self cm_CreateButtonWithTitle:title font:font titleColor:titColor image:nil selectedImage:nil bgColor:[UIColor clearColor]];
}

+ (UIButton*) cm_ButtonWithTitle:(NSString*) title
                            font:(UIFont*) font
                      titleColor:(UIColor*) titColor
                         bgColor:(UIColor*) bgColor
{
    return [self cm_CreateButtonWithTitle:title font:font titleColor:titColor image:nil selectedImage:nil bgColor:bgColor];
}

+ (UIButton*) cm_ButtonWithTitle:(NSString*) title
                            font:(UIFont*) font
                      titleColor:(UIColor*) titColor
                           image:(UIImage*) normalImage
{
    return [self cm_CreateButtonWithTitle:title font:font titleColor:titColor image:normalImage selectedImage:nil bgColor:[UIColor clearColor]];
}

+ (UIButton*) cm_CreateButtonWithTitle:(NSString*) title
                                  font:(UIFont*) font
                            titleColor:(UIColor*) titColor
                                 image:(UIImage*) norImage
                         selectedImage:(UIImage*) selectImage bgColor:(UIColor*) bgColor
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = font;
    [button setTitleColor:titColor forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    if(norImage){
        [button setImage:norImage forState:UIControlStateNormal];
    }
    if(selectImage){
        [button setImage:selectImage forState:UIControlStateSelected];
    }
    if(bgColor != [UIColor clearColor]){
        [button setBackgroundColor:bgColor];
    }
    return button;
}


- (void)setImagePosition:(YDImagePosition)postion spacing:(CGFloat)spacing {
    [self setTitle:self.currentTitle forState:UIControlStateNormal];
    [self setImage:self.currentImage forState:UIControlStateNormal];
    
    CGFloat imageWidth = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;
    
    CGFloat labelWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
    CGFloat labelHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height;
    
    CGFloat imageOffsetX = (imageWidth + labelWidth) / 2 - imageWidth / 2;//image中心移动的x距离
    CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;//image中心移动的y距离
    CGFloat labelOffsetX = (imageWidth + labelWidth / 2) - (imageWidth + labelWidth) / 2;//label中心移动的x距离
    CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;//label中心移动的y距离
    
    CGFloat tempWidth = MAX(labelWidth, imageWidth);
    CGFloat changedWidth = labelWidth + imageWidth - tempWidth;
    CGFloat tempHeight = MAX(labelHeight, imageHeight);
    CGFloat changedHeight = labelHeight + imageHeight + spacing - tempHeight;
    
    switch (postion) {
        case Left:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
            
        case Right:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + spacing/2, 0, -(labelWidth + spacing/2));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth + spacing/2), 0, imageWidth + spacing/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
            
        case Top:
            self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(imageOffsetY, -changedWidth/2, changedHeight-imageOffsetY, -changedWidth/2);
            break;
            
        case Bottom:
            self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -labelOffsetX, labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(changedHeight-imageOffsetY, -changedWidth/2, imageOffsetY, -changedWidth/2);
            break;
            
        default:
            break;
    }
    
}
@end
